<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
/*
 * riverside-anywhere - a useful tool for riverside of UESTC
 * Copyright (C) 2011-2012, riverside-anywhere developers and contributors
 * Copyright (C) 2011-2012, Cohesion Network Security Studio
 *
 * riverside-anywhere is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * riverside-anywhere is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with riverside-anywhere; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

// when load background.html, check if user at school
$.get("http://bbs.stuhome.net",function(data){
	if(data.search(/<strong>UESTC internal IP<\/strong>/) != -1) {
		localStorage['atSchool'] = 0;
	} else {
		localStorage['atSchool'] = 1;
	}
	return;
});

// when access stuhome.net, redirect to qshpan.com
chrome.webRequest.onBeforeRequest.addListener(
	function(details) {
		// if user at school, don't redirect url
		if(localStorage['atSchool'] == 1) {
			return;
		}
		var redirectUrl;
		if(details.url.match("bbs.stuhome.net") != null) {
			redirectUrl = details.url.replace(/bbs\.stuhome\.net/,"bbs.qshpan.com");
		} else if(details.url.match("bbs.uestc6.edu.cn") != null) {
			redirectUrl = details.url.replace(/bbs\.uestc6\.edu\.cn/,"bbs.qshpan.com");
		} else {
			redirectUrl = detail.url;
		}

		return {
			redirectUrl: redirectUrl
		};
	},
	{
		urls: [
			"http://bbs.stuhome.net/*",
			"http://bbs.uestc6.edu.cn/*"
		]
	},
	["blocking"]
);

// get user verifyHash and winduid
// only called when first load extension / change account / logout
function getId() {
	var url;
	if(localStorage['atSchool'] == 1) {
		url = "http://bbs.stuhome.net";
	} else {
		url = "http://bbs.qshpan.com";
	}
	$.get(url,function(data){
		var str = data.match(/verifyhash = '.*';/g)[0];
		var start = str.indexOf("'");
		var end = str.lastIndexOf("'");
		localStorage['verifyHash'] = str.slice(start + 1,end);

		var str = data.match(/winduid = '.*';/g)[0];
		var start = str.indexOf("'");
		var end = str.lastIndexOf("'");
		localStorage['winduid'] = str.slice(start + 1,end);

		return;
	});
}

// when load extension, get user verifyHash and winduid
getId();

function getMsgCount() {
	if(localStorage['verifyHash'] == undefined
	|| localStorage['verifyHash'] == ""
	|| localStorage['winduid'] == undefined
	|| localStorage['winduid'] == "") {
		// if not login, show "?" and refresh id
		chrome.browserAction.setBadgeText({text: "?"});
		getId();
		return;
	} else {
		chrome.browserAction.setBadgeText({text: ""});
	}

	// build ajax query url
	var url;
	if(localStorage['atSchool'] == 1) {
		url = "http://bbs.stuhome.net";
	} else {
		url = "http://bbs.qshpan.com";
	}
	url += "/pw_ajax.php?action=pwb_message"
	+ "&nowtime=" + (new Date()).getTime()
	+ "&verify=" + localStorage['verifyHash'];
	$.post(url,{u: localStorage['winduid']},function(data) {
		// get return data to content
		var text = data.childNodes[0].childNodes[0].textContent;
		var content = text.split("\t");
		if(content[0] == 'success') {
			var msg = $.parseJSON(content[1]);
			if(msg != null && msg.length != 0) {
				chrome.browserAction.setBadgeText({text: msg.length.toString()});
			} else {
				chrome.browserAction.setBadgeText({text: ""});
			}
		} else {
			// if not success, probably logout or change account, need refresh localStorage
			getId();
			chrome.browserAction.setBadgeText({text: ""});
		}
	});
}

getMsgCount();

// auto get count of recive message every 15 seconds
setInterval(getMsgCount,15000);

// click icon will create a new tab for stuhome.net
chrome.browserAction.onClicked.addListener(function(tab) {
	chrome.tabs.create({
		url: "http://bbs.stuhome.net",
		active: true,
		pinned: false
	});
});
</script>
</head>
</html>
